package com.gitee.empty_null.authorization.authentication;

import org.springframework.lang.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationGrantAuthenticationToken;
import org.springframework.util.Assert;

import java.io.Serial;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author xuhainan
 * @date 2024/1/15 16:44
 * @region hefei
 */
public class OAuth2UsernamePasswordAuthenticationToken extends OAuth2AuthorizationGrantAuthenticationToken {
    @Serial
    private static final long serialVersionUID = -4784288991943354925L;

    @SuppressWarnings("deprecation")
    private static final AuthorizationGrantType AUTHORIZATION_GRANT_TYPE = AuthorizationGrantType.PASSWORD;
    private final Set<String> scopes;

    public OAuth2UsernamePasswordAuthenticationToken(Authentication clientPrincipal,
                                                     @Nullable Set<String> scopes,
                                                     @Nullable Map<String, Object> additionalParameters) {
        super(AUTHORIZATION_GRANT_TYPE, clientPrincipal, additionalParameters);
        Assert.notNull(clientPrincipal, "clientPrincipal cannot be null");
        this.scopes = Collections.unmodifiableSet(
                scopes != null ? new HashSet<>(scopes) : Collections.emptySet());
    }

    public Set<String> getScopes() {
        return this.scopes;
    }

}
